import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_09_game/pages/game/game_detail/models/model_quick_bet_set.dart';
import 'package:flutter_09_game/pages/splash/model_api_config.dart';
import 'package:flutter_09_game/utils/shared/shared_export.dart';

import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:url_launcher/url_launcher_string.dart';

class AppConfig {
  static String baseUrl = "";
  static String baseImage = '';
  static String wsUrl = '';
  static String shareUrl = '';

  //测试
  // static String baseUrl = "https://api.798ys.xyz";
  // static String baseImage = 'https://back.798ys.xyz';
  // static String wsUrl = 'wss://api.798ys.xyz/api/ws/';
  // static String shareUrl = 'https://web.798ys.xyz';

//正式
//   static String baseUrl = "https://api.fxkd100.com";
//   static String baseImage = 'https://file.fxkd100.com';
//   static String wsUrl = 'wss://api.fxkd100.com/api/ws/';
//   static String shareUrl = 'http://web-begin.miaofufu.com';

  static String shareUrlCenter = '/login?action=register&invite_code=';

  // static ModelApiConfig defaultApiConfig = ModelApiConfig(api: [
  //   "https://api.fxkd100.com",
  // ], file: [
  //   // AppConfig.baseImage
  //   'https://file.fxkd100.com'
  // ], websocket: [
  //   // AppConfig.wsUrl
  //   'wss://api.fxkd100.com'
  //   // static String wsUrl = 'wss://api.798ys.xyz/api/ws/';
  // ], frontend: [
  //   'https://game-start.u9game.cn'
  // ], pool: [
  //   'https://pool.798ys.xyz/'
  // ]);

  //测试
  // static ModelApiConfig defaultApiConfig = ModelApiConfig(api: [
  //   "https://api.798ys.xyz",
  // ], file: [
  //   // AppConfig.baseImage
  //   'https://back.798ys.xyz'
  // ], websocket: [
  //   // AppConfig.wsUrl
  //   'wss://api.798ys.xyz'
  //   // static String wsUrl = 'wss://api.798ys.xyz/api/ws/';
  // ], frontend: [
  //   'https://web.798ys.xyz'
  // ], pool: [
  //   'https://pool.798ys.xyz/'
  // ]);

//正式
  static ModelApiConfig defaultApiConfig = ModelApiConfig(api: [
    "https://api.hlyanjing.com",
    "https://api.haojc04.com"
  ], file: [
    "https://file.hlyanjing.com",
    "https://file.haojc04.com"
  ], websocket: [
    "wss://wss.hlyanjing.com",
    "wss://wss.haojc04.com"
    // static String wsUrl = 'wss://api.798ys.xyz/api/ws/';
  ], frontend: [
    "https://web-begin.hlyanjing.com",
    "https://web-begin.lanyou188.com"
  ], pool: [
    "https://pool.gxrzf56.com/json",
    "https://pool.1mfksb.com/json",
    "https://pool.iwupha.com/json"
  ]);

  static String verifyUrl = kDebugMode
      ? 'https://shasta.tronscan.org/#/block/'
      : 'https://tronscan.org/#/block/';

  static String appName = '';
  static String version = '';

  static String deviceId = '';

  static List<ModelQuickBetSet> quickDefines = [
    ModelQuickBetSet(amount: 20000000),
    ModelQuickBetSet(amount: 100000000),
    ModelQuickBetSet(amount: 500000000),
    ModelQuickBetSet(amount: 1000000000),
    ModelQuickBetSet(amount: 5000000000),
  ];

  // static int serviceDifference = 0;

  static Map<String, dynamic> settingConfigs = {};

  static Future setup() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    appName = packageInfo.appName;
    version = packageInfo.version;
    _initDeviceId();
  }

  // 初始化设备ID
  static Future<void> _initDeviceId() async {
    // 首先尝试从SharedPreferences获取存储的设备ID
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? storedDeviceId = prefs.getString(SharedKey.deviceId);

    if (storedDeviceId != null && storedDeviceId.isNotEmpty) {
      deviceId = storedDeviceId;
      return;
    }

    // 如果没有存储的设备ID，则生成新的设备ID
    final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

    try {
      if (Platform.isAndroid) {
        // Android设备ID
        final AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
        deviceId = androidInfo.id; // 使用Android ID
      } else if (Platform.isIOS) {
        // iOS设备ID
        final IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
        deviceId = iosInfo.identifierForVendor ?? ''; // 使用identifierForVendor
      }

      // 存储设备ID以便下次使用
      await prefs.setString(SharedKey.deviceId, deviceId);
    } catch (e) {
      print('获取设备ID失败: $e');
      // 生成一个随机ID作为后备方案
      deviceId = DateTime.now().millisecondsSinceEpoch.toString();
      await prefs.setString(SharedKey.deviceId, deviceId);
    }
  }

  static String? getServiceUrl() {
    if (settingConfigs.keys.contains('PromotionURLKey')) {
      return settingConfigs['PromotionURLKey'];
    } else {
      return null;
    }
  }
}
